<template>
	 <up-upload
	  	:fileList="fileList2"
	  	@afterRead="afterRead2"
	  	@delete="deletePic2"
	  	name="2"
	  	multiple
	  	:maxCount="1"
	  	accept="video"
	  ></up-upload>
</template>

<script setup>
	import {ref} from "vue"
	import { defineProps, defineEmits } from 'vue';
	let fileList2 = ref([])
	let fileList_video = ref([])
	let props = defineProps(['getvideos'])
	// 删除图片
	const deletePic2 = (event) => {
	  fileList2.value.splice(event.index, 1);
	};
	
	// 新增图片
	const afterRead2 = async (event) => {
	  // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
	  let lists = [].concat(event.file);
	  let fileListLen = fileList2.value.length;
	  lists.map((item) => {
	    fileList2.value.push({
	      ...item,
	      status: 'uploading',
	      message: '上传中',
	    });
	  });
	  for (let i = 0; i < lists.length; i++) {
	    const result = await uploadFilePromise(lists[i].url);
	    let item = fileList2.value[fileListLen];
	    fileList2.value.splice(fileListLen, 1, {
	      ...item,
	      status: 'success',
	      message: '',
	      url: result,
	    });
	    fileListLen++;
	  }
	};
	
	const uploadFilePromise = (url) => {
	  return new Promise((resolve, reject) => {
	    let a = uni.uploadFile({
	      url: 'http://47.93.162.7:3000/upload',
	      filePath: url,
	      name: 'file',
	      formData: {
	        user: 'test',
	      },
	      success: (res) => {
	        setTimeout(() => {
	          resolve(res.data.data);
			  let a = JSON.parse(res.data).path
			  let newa = a.replace(/\\/g, '/');
			  console.log(newa);
			  fileList_video.value.push(newa)
			  props.getvideos(fileList_video.value)
	        }, 1000);
	      },
	    });
	  });
	};
</script>

<style>
</style>